package ui.components
{
	import events.GestureEvent;
	
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.text.TextField;
	
	import global.Managers;
	
	import resources.Resources;
	
	import utils.DisplayUtil;
	import utils.TextUtil;
	
    public class Button extends AbstractComponent
    {
        protected var _width:Number;
        protected var _height:Number;
        protected var _text:String;
        protected var _tip:String;
        protected var _image:Bitmap;
        public var btnData:Object;
        protected var _imageURL:String;
        protected var _label:TextField;
        protected var _imageEmbeded:DisplayObject;
        protected var _function:Function;

        public function Button(id:int = -1, text:String = "", width:Number = -1, height:Number = -1, image:String = "", callback:Function = null)
        {
            _function = callback;
            _imageURL = image;
            _text = text;
            _width = width;
            _height = height;
            super(id, callback);
            init();
        }

        public function setSize(param1:Number, param2:Number) : void
        {
            _width = param1;
            _height = param2;
            drawLayout();
        }

        override public function set enable(param1:Boolean) : void
        {
            super.enable = param1;
            this.buttonMode = param1;
            if (param1)
            {
                this._func = _function;
            }
            else
            {
                this._func = null;
            }
        }

        public function set title(param1:String) : void
        {
            if (_label == null)
            {
                _label = TextUtil.getGGTextField();
            }
            _label.text = param1;
            DisplayUtil.addChild(_label, this);
            drawLayout();
        }

        public function set callBack(param1:Function) : void
        {
            _function = param1;
            this._func = _function;
        }

        override public function clear() : void
        {
            this._id = -1;
            this._imageURL = "";
            this._func = null;
            this._tip = "";
        }

        private function drawLayout() : void
        {
            if (this.contains(_label))
            {
                _width = _label.width > _width ? (_label.width + 8) : (_width);
                _height = _label.height > _height ? (_label.height + 6) : (_height);
                _label.x = (_width - _label.width) / 2;
                _label.y = (_height - _label.height) / 2;
            }
            else
            {
                _width = _width == -1 ? (_image.width) : (_width);
                _height = _height == -1 ? (_image.height) : (_height);
            }
            _image.width = _width;
            _image.height = _height;
            _imageEmbeded.width = _width;
            _imageEmbeded.height = _height;
        }

        protected function init() : void
        {
            initImage();
            initText();
            this.mouseChildren = false;
            this.buttonMode = true;
            this.useHandCursor = true;
            drawLayout();
        }

        protected function initText() : void
        {
            _label = TextUtil.getGGTextField();
            if (_text != null && _text != "")
            {
                _label.text = _text;
                DisplayUtil.addChild(_label, this);
            }
        }

        public function set text(param1:String) : void
        {
            _text = param1;
            _label.text = _text;
            if (!this.contains(_label))
            {
                addChild(_label);
            }
            drawLayout();
        }

        override protected function clickHandler(event:GestureEvent) : void
        {
            if (enable)
            {
                super.clickHandler(event);
            }
        }

        public function resetImage(url:String) : void
        {
            this._imageURL = url;
            _image.bitmapData = Managers.resourceManager.getBitmapData(url);
            _image.visible = true;
            _imageEmbeded.visible = false;
            drawLayout();
        }

        public function get callBack() : Function
        {
            return _function;
        }

        protected function initImage() : void
        {
            _image = new Bitmap();
            if (_imageURL == null || _imageURL == "")
            {
                _image.bitmapData = Managers.resourceManager.getBitmapData(Resources.BUTTON_ORANGE);
            }
            else
            {
                _image.bitmapData = Managers.resourceManager.getBitmapData(_imageURL);
            }
            addChild(_image);
            _imageEmbeded = new Bitmap();
            addChild(_imageEmbeded);
        }

        public function resetEmbededImage(param1:Class) : void
        {
            DisplayUtil.removeChild(_imageEmbeded, this);
            _imageEmbeded = new param1;
            _image.visible = false;
            _imageEmbeded.visible = true;
            DisplayUtil.addChild(_imageEmbeded, this);
            drawLayout();
        }

        public function get text() : String
        {
            return _text;
        }

        public function get tips() : String
        {
            return _tip;
        }
    }
}